package com.eling.elcms.travel.dao.hibernate;

import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.travel.dao.IReturnCredentialsDao;
import com.eling.elcms.travel.model.ReturnCredentials;
import com.eling.elcms.travel.model.view.ReturnCredentialsView;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class ReturnCredentialsDaoHibernate extends GenericDaoHibernate<ReturnCredentials, Long> implements IReturnCredentialsDao{
	public ReturnCredentialsDaoHibernate() {
		super(ReturnCredentials.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ReturnCredentialsView> exitQuery(String status) {
		StringBuffer sql = new StringBuffer();
		sql.append("  SELECT  ");
		sql.append("  rc.pkReturnCredentials,tc.memberCertificates,tc.contactNo,per. NAME AS perNAme, per.idNumber,pro. NAME AS proName,  ");
		sql.append("  pt. NAME AS ptName,tc.number,rc.operateDate AS 申请日期,rc.price,mem. NAME AS residenceName,rc.status,rc.flatCost AS 工本费,  ");
		sql.append("  tc.price AS totalPrice, rc.defaultRatio,rc.cause,tc.signUpDate,tc.balance,tc. STATUS AS tcStatus,mem.phone ");
		sql.append("  FROM  ");
		sql.append("  travel_returncredentials rc  ");
		sql.append("  INNER JOIN travel_travelcontract tc ON rc.pkTravelContract = tc.pkTravelContract  ");
		sql.append("  INNER JOIN travel_productrule pr ON pr.pkProduct = tc.pkProductRule  ");
		sql.append("  INNER JOIN travel_product pro ON pro.pkProduct = pr.pkProduct  ");
		sql.append("  INNER JOIN travel_producttype pt ON pt.pkProductType = tc.pkProductType  ");
		sql.append("  INNER JOIN mem_member mem ON mem.pkMember = rc.operator  ");
		sql.append("  INNER JOIN bd_personalinfo per ON per.pkPersonalInfo = mem.pkPersonalInfo  ");
		sql.append("  WHERE  ");
		sql.append("  rc.status like'%"+status+"'");
		sql.append("  GROUP BY  tc.contactNo ");
		
		List<Object[]> objList = getSession().createNativeQuery(sql.toString()).list();
		
		List<ReturnCredentialsView> rcwlist = new ArrayList<>();
		
		for(int i =0;i<objList.size();i++){
			
			ReturnCredentialsView rcw = new ReturnCredentialsView();
			Object[] objects = objList.get(i);
//			TravelContract travelContract = new TravelContract();
			rcw.setPkReturnCredentials(new Long(objects[0].toString()));
				rcw.setMemberCertificates(objects[1]==null? null: objects[1].toString());
				rcw.setContactNo(objects[2]==null ? null : objects[2].toString());
				rcw.setNumber(objects[7]==null?0:new Integer(objects[7].toString()));
//			rcw.setTravelContract(travelContract==null? null:travelContract);
			rcw.setPersonName(objects[3]==null? null:objects[3].toString());
			rcw.setPersonId(objects[4]==null? null: objects[4].toString());
			rcw.setProductName(objects[5]==null? null:objects[5].toString());
			rcw.setProductTypeName(objects[6]==null?null:objects[6].toString());
			rcw.setOperateDate(objects[8]==null? null:(Date)objects[8]);
			rcw.setPrice(objects[9]==null?0.00:new Double(objects[9].toString()));
			rcw.setOperater(objects[10]==null? null:objects[10].toString());
			rcw.setStatus(objects[11]==null? null:objects[11].toString());
			rcw.setFlatCost(objects[12]==null?0.00:new Double(objects[12].toString()));
			rcw.setTotalPrice(objects[13]==null?0.00:Double.parseDouble((objects[13].toString())));
			rcw.setDefaultRatio(objects[14]==null?0.00: new Double(objects[14].toString()));
			rcw.setCause(objects[15]==null?null:objects[15].toString());
			rcw.setSignUpDate(objects[16]==null?null:(Date)objects[16]);
			rcw.setBalance(objects[17]==null?0.00:new Double(objects[17].toString()));
			rcw.setTcStatus(objects[18]==null? null: objects[18].toString());			
			rcw.setPhone(objects[19]==null?null:objects[19].toString());
			
			rcwlist.add(rcw);
		}
		return rcwlist;
	}
}
